package com.hq.xmind.service;

import com.hq.xmind.model.Knowledge;
import com.hq.xmind.model.Relation;
import com.hq.xmind.model.ZNodeTree;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.xmind.core.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XmindService {

    public String getTreeJson() {

        IWorkbookBuilder builder = Core.getWorkbookBuilder();//初始化builder
        IWorkbook workbook = null;
        try {
            File file = ResourceUtils.getFile("classpath:file/xxxx.xmind");
            workbook = builder.loadFromPath(file.getAbsolutePath());//打开XMind文件
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CoreException e) {
            e.printStackTrace();
        }
        ISheet defSheet = workbook.getPrimarySheet();//获取主Sheet
        ITopic rootTopic = defSheet.getRootTopic();//获取根Topic
        System.out.println(rootTopic);
        System.out.println(rootTopic.getChildrenTypes());
        System.out.println(defSheet.getRelationships());
        List<ITopic> allChildren = rootTopic.getAllChildren();
        int code = 1;
        for (ITopic c :allChildren) {
            c.setStyleId("ABC_00"+code);
            code++;
//            System.out.println(c.getStyleId());
        }
        ZNodeTree treeRoot = new ZNodeTree();
        treeRoot.setId(rootTopic.getId());
        treeRoot.setName(rootTopic.getTitleText());
        treeRoot.setPathNameList(rootTopic.getTitleText());
        treeRoot.setPathIds(treeRoot.getId());
        treeRoot.setpId("root");
        treeRoot.setLevel(0);
        List<ZNodeTree> treeList = new ArrayList<ZNodeTree>();
        treeList.add(treeRoot);
        List<Relation> relationList = new ArrayList<>();
        List<Knowledge> knowledgeList = new ArrayList<>();
        Map<String,ZNodeTree> mapPath = new HashMap<>();
        mapPath.put(treeRoot.getId(),treeRoot);
        treeTopicList(mapPath,allChildren, 1, treeRoot.getId(), treeList,relationList,knowledgeList);
//        String json = JSONArray.fromObject(treeList).toString();
        System.out.println(treeList);
        return null;
    }

    private static String code;


    public static List<ZNodeTree> treeTopicList(Map<String,ZNodeTree> mapPath,List<ITopic> allChildren, int level, String id,  List<ZNodeTree> treeList,List<Relation> relationList,List<Knowledge> knowledgeList) {
        for (ITopic topic : allChildren) {

            ZNodeTree zNodeTree = mapPath.get(id);

            String ids = zNodeTree.getPathIds() + ","+topic.getId();
            String names = zNodeTree.getPathNameList() + ","+topic.getTitleText();


            List<ITopic> childrenList = topic.getAllChildren();

            ZNodeTree tree = new ZNodeTree();
            tree.setId(topic.getId());
            tree.setParentName(topic.getParent().getTitleText());
            tree.setpId(topic.getParent().getId());
            tree.setLevel(level);
            tree.setName(topic.getTitleText());
            tree.setPathIds(ids);
            tree.setPathNameList(names);
            treeList.add(tree);
            mapPath.put(topic.getId(),tree);

            System.out.println(tree);

//            Relation relation = new Relation();
//
//            relation.setRelation_type(1);
//            relation.setLevel(1);
//
//            Knowledge knowledge  = new Knowledge();
//
//            knowledge.setKg_xmind_id(topic.getId());
//
////            System.out.println(topic);
//            knowledge.setName(topic.getTitleText());
//            knowledge.setKg_path(name);
//            if(level==1){
//                code=topic.getStyleId();
//                knowledge.setKg_id(code+"_"+topic.getId());
////                relation.setPre_kg_id(topic.getParent().getId());
////                relation.setPos_kg_id(topic.getId());
//            }else{
//                knowledge.setKg_id(code+"_11_"+topic.getId());
//            }
//            knowledgeList.add(knowledge);
            if (childrenList.size() > 0) {
                treeTopicList(mapPath,childrenList, level + 1, topic.getId(), treeList,relationList,knowledgeList);
            }

        }
        return treeList;
    }


}
